import type { FieldRule, FormInstance } from '@arco-design/web-vue'

export type CascadeLevel = 1 | 2

interface IFormData {
  cache_path: string
  cache_size: number
  cascade_ip: string
  cascade_level: CascadeLevel
  cascade_port: number
  id: string
  ips: string[]
  name: string
  region_id: string
}

// const validateInteger: FieldRule['validator'] = (value, callback) => {
//   const valueStr = value.toString()
//   if (valueStr.includes('.')) {
//     return callback('请输入正整数')
//   }
//   callback()
// }

export default function useNodeTemplateConfig<T extends object>(otherData: T) {
  const diskFileSizeUnit = ref(2)
  const formRef = ref<FormInstance>()
  const formData = reactive<IFormData & { [key in keyof T]: T[key] }>({
    ...otherData,
    cache_path: '',
    cache_size: 200,
    cascade_ip: '',
    cascade_level: 1,
    cascade_port: 0,
    id: '',
    ips: [],
    name: '',
    region_id: ''
  })
  const formRules = computed<Record<string, FieldRule[] | FieldRule>>(() => {
    return {
      cache_path: [
        { required: true, message: '请输入文件缓存路径' },
        {
          validator(value, callback) {
            if (!value.startsWith('/')) {
              return callback('请输入以/开头的绝对路径')
            }
            callback()
          }
        }
      ],
      cache_size: cacheSizeRule(diskFileSizeUnit.value)
    }
  })

  return {
    formRef,
    formData,
    formRules,
    diskFileSizeUnit
  }
}
